# Replication Archive for: 
# Coppock, Alexander and Donald P. Green. 2020. 
# "Do Belief Systems Exhibit Dynamic Constraint?" 
# The Journal of Politics, Forthcoming.

rm(list = ls())

library(tidyverse)
library(xtable)

study_1_mturk <- read_rds("data/clean/study_1_mturk_cleaned.rds")
study_1_elite <- read_rds("data/clean/study_1_elite_cleaned.rds")
study_1_lucid <- read_rds("data/clean/studies_1_2_replications_lucid_cleaned.rds")

study_1_mturk <- study_1_mturk %>% filter(Z != "climate")
study_1_lucid <- study_1_lucid %>% filter(Z != "climate")

dv_df <- read_rds("data/raw/dv_df.rds")
dv_df_mturk <- filter(dv_df, dv_cat != "climate")
dv_df_elite <- filter(dv_df, dv_cat != "climate")
dv_df_lucid <- filter(dv_df, dv_cat != "climate")

# Correlations ------------------------------------------------------------

mturk_pairs <- expand.grid(paste0(dv_df_mturk$dvs, "_w1"), paste0(dv_df_mturk$dvs, "_w1"))

mturk_cors <-
  map2_dbl(
    .x = as.character(mturk_pairs[, 1]),
    .y = as.character(mturk_pairs[, 2]),
    .f = ~ cor(study_1_mturk[, .x], study_1_mturk[, .y], use = "complete.obs")
  )

mturk_df <-
  cbind(mturk_pairs, mturk_cors) %>%
  left_join((dv_df_mturk %>% transmute(
    Var1 = paste0(dvs, "_w1"), Var1_cat = dv_cat
  ))) %>%
  left_join((dv_df_mturk %>% transmute(
    Var2 = paste0(dvs, "_w1"), Var2_cat = dv_cat
  )))


elite_pairs <-
  expand.grid(paste0(dv_df_elite$dvs, "_w1"),
              paste0(dv_df_elite$dvs, "_w1"))

elite_cors <-
  map2_dbl(
    .x = as.character(elite_pairs[, 1]),
    .y = as.character(elite_pairs[, 2]),
    .f = ~ cor(study_1_elite[, .x], study_1_elite[, .y], use = "complete.obs")
  )


elite_df <-
  cbind(elite_pairs, elite_cors) %>%
  left_join((dv_df_elite %>% transmute(
    Var1 = paste0(dvs, "_w1"), Var1_cat = dv_cat
  ))) %>%
  left_join((dv_df_elite %>% transmute(
    Var2 = paste0(dvs, "_w1"), Var2_cat = dv_cat
  )))

lucid_pairs <-
  expand.grid(paste0(dv_df_lucid$dvs, "_w1"),
              paste0(dv_df_lucid$dvs, "_w1"))

lucid_cors <-
  map2_dbl(
    .x = as.character(lucid_pairs[, 1]),
    .y = as.character(lucid_pairs[, 2]),
    .f = ~ cor(study_1_lucid[, .x], study_1_lucid[, .y], use = "complete.obs")
  )


lucid_df <-
  cbind(lucid_pairs, lucid_cors) %>%
  left_join((dv_df_lucid %>% transmute(
    Var1 = paste0(dvs, "_w1"), Var1_cat = dv_cat
  ))) %>%
  left_join((dv_df_lucid %>% transmute(
    Var2 = paste0(dvs, "_w1"), Var2_cat = dv_cat
  )))



long_df <-
  bind_rows(list(
    `MTurk Sample` = rename(mturk_df, cor = mturk_cors),
    `Elite Sample` = rename(elite_df, cor = elite_cors),
    `Lucid Sample` = rename(lucid_df, cor = lucid_cors)
  ),
  .id = "study") %>%
  mutate(same_dv_cat = Var1_cat == Var2_cat,
         study = factor(study, levels = c("MTurk Sample",
                                          "Elite Sample",
                                          "Lucid Sample"))) %>%
  filter(Var1 != Var2)


tab_df <-
  long_df %>%
  group_by(study, same_dv_cat) %>%
  summarise(avg_cor = mean(cor)) %>%
  spread(key = same_dv_cat, value = avg_cor)

xtable(tab_df) %>%
  print.xtable(include.colnames = FALSE,
               include.rownames = FALSE,
               hline.after = c(),
               only.contents = TRUE,
               file = "../Drafts/tables/static_cor.tex")
